<?php
namespace app\api\controller\service;

use app\common\controller\Api;
use fast\Random;
use think\Db;


/**
 * 首页接口
 */
class Plus extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function plus()
    {
        $uid = $this->auth->id;
        $userInfo = model('app\api\model\service\UserInfo')->where(['user_id'=>$uid])->field('id,is_plus,plusname,plustime')->find();
        $type = input('type','');
        if($type == 'pay')
        {
            $id = input('id/d','');
            $paytype = input('paytype/d',0);
            $info = model('app\api\model\service\PlusConfig')->where(['id'=>$id])->field('id,price,name')->find();
            !$info && $this->error('参数错误');
            $price = sprintf("%.2f",$info['price']);
            $orderId = 'PlusPay'.Random::alnum(4).'-'.$uid.'-'.time();
            $re = '';
            Db::startTrans();
            try{
                $skillEnsurePay = new \app\api\model\service\PlusPay(['user_id'=>$uid,'price'=>$price,'plus_id'=>$id,'orderId'=>$orderId,'paytype'=>$paytype]);
                $skillEnsurePay->allowField(true)->save();
                $re = \addons\service\library\Pay::payOrder(['amount'=>$price,'orderid'=>$orderId,'title'=>'充值会员'],$paytype,$uid,0);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('支付拉起失败',$e->getMessage());
            }
            $this->success('支付信息返回成功',$re);
        }
        $list = model('app\api\model\service\PlusConfig')->field('id,name,price,original_price,daynums,discount')->order('price asc')->select();
        $this->success('信息返回成功',$list);
    }
}
